<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Country_model  extends MY_Model  {

    public $table = 'countries';

	public function __construct() {
		$this->class_name = get_class ( $this );
		parent::__construct( $this->class_name, $this->table );
		parent::init( $this->class_name, $this->table );
	}
	
    /**
     * Get entries by filter array
     * @param mixed $filter
     * @return mixed
     */
    function get_by_params($filter=array()) {
        if (isset($filter['field']) AND strlen($filter['field'])) {
            $this->db->select($filter['field']);
        }
        else {
            $this->db->select('*',TRUE);
        }
        
        if (isset($filter['name']) AND strlen($filter['name'])) {
            $this->db->where('name',$filter['name']);
        }
        if (isset($filter['language_id']) AND strlen($filter['language_id'])) {
            $this->db->where('language_id',$filter['language_id']);
        }
        	
        $order = 'name';
        $dir = 'ASC';
        if (isset($filter['order_by']) AND strlen($filter['order_by'])){
            $order = $filter['order_by'];
        }
        if (isset($filter['dir']) AND strlen($filter['dir'])){
            $dir = $filter['dir'];
        }
        $this->db->order_by ( $order, $dir );
        $this->db->from($this->table);
        $query = $this->db->get();
        return $query;
    } 	
}